﻿using Error_Handler_Control;
using PollSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PollSystem.Registered
{
    public partial class EditAnswer : System.Web.UI.Page
    {
        private int questionId;
        private int answerId;
        private bool isNewAnswer;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.questionId = Convert.ToInt32(Request.Params["questionId"]);
            this.answerId = Convert.ToInt32(Request.Params["answerId"]);
            isNewAnswer = (this.answerId == 0);
            ViewState["PreviousPage"] = Request.UrlReferrer;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!isNewAnswer)
            {
                using (PollSystemEntities context = new PollSystemEntities())
                {
                    Answer answer = context.Answers.Find(answerId);
                    this.TextBoxEditAnswer.Text = answer.AnswerText;
                    this.TextBoxEditVotes.Text = answer.Votes.ToString();
                }
            }
        }

        protected void LinkButtonSaveEditedAnswer_Click(object sender, EventArgs e)
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                Answer answer;
                answer = context.Answers.Find(this.answerId);
                if (!string.IsNullOrEmpty(this.TextBoxEditAnswer.Text))
                {
                    answer.AnswerText = this.TextBoxEditAnswer.Text;
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Please enter answer.");
                    return;
                }
                if (!string.IsNullOrEmpty(this.TextBoxEditVotes.Text))
                {
                    try
                    {
                        answer.Votes = int.Parse(this.TextBoxEditVotes.Text);
                    }
                    catch (Exception ex)
                    {
                        ErrorSuccessNotifier.AddErrorMessage("Please enter number >= 0");
                        return;
                    }
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Please enter number >= 0");
                    return;
                }
                try
                {
                    context.SaveChanges();
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    ErrorSuccessNotifier.AddSuccessMessage("Answer edited.");
                    Response.Redirect("EditQuestion.aspx?questionId=" +
                        answer.QuestionId, false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }

        protected void LinkButtonCancel_Click(object sender, EventArgs e)
        {
            ErrorSuccessNotifier.ShowAfterRedirect = true;
            ErrorSuccessNotifier.AddSuccessMessage("Answer editing canceled.");
            Response.Redirect(ViewState["PreviousPage"].ToString());
        }
    }
}